package com.soft.javase.day10.practice.streamdemo2;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author yzg
 * @desc TODO
 * @date 2025/5/4
 */
public class StreamDemo2 {
    public static void main(String[] args) {
        //筛选出 Integer 集合中大于 7 的元素
        List<Integer> numbers = Arrays.asList(6, 7, 3, 8, 1, 2, 9);
        List<Integer> result = numbers.stream().filter(n -> n > 7).collect(Collectors.toList());
        System.out.println(result);

        //筛选员工中工资高于8000的人，并形成新的集合
        List<Employee> employee = new ArrayList<>();
        employee.add(new Employee("张三", 7000.0,18));
        employee.add(new Employee("李四", 8500.0,20));
        employee.add(new Employee("王五", 9000.0,21));
        employee.add(new Employee("赵六", 7500.0,19));
        employee.add(new Employee("小明", 8500.0,19));

        List<Employee> result1 = employee.stream()
                .filter(e -> e.getSalary() > 8000)
                .collect(Collectors.toList());

        System.out.println("工资高于8000的员工:" +result1);

        //获取String集合中最长的元素
        List<String> string = Arrays.asList("Java", "Python", "C++", "JavaScript", "C");

        Optional<String> longest = string.stream()
                .reduce((s1, s2) -> s1.length() > s2.length() ? s1 : s2);
        System.out.println(longest.orElse(""));


        //获取Integer集合中最大值
        List<Integer> numbers1 = Arrays.asList(3, 9, 1, 7, 5, 11, 4);
        Optional<Integer> max = numbers1.stream().reduce(Integer::max);
        System.out.println(max.orElse(0));

        //获取员工工资最高的人
        Optional<Employee> highest = employee.stream()
                .reduce((e1, e2) -> e1.getSalary() > e2.getSalary() ? e1 : e2);
        System.out.println(highest.orElse(null));


        //计算Integer集合中大于6的元素的个数
        List<Integer> numbers2 = Arrays.asList(6, 7, 3, 8, 1, 2, 9);

        System.out.println("大于6的元素个数: " + numbers2.stream()
                .filter(n -> n > 6)
                .count());


        //将员工按照工资由低到高排序，显示名字
        System.out.println("员工按工资由低到高排序:");
        employee.stream()
                .sorted(Comparator.comparingDouble(Employee::getSalary))
                .map(Employee::getName)
                .forEach(System.out::println);

        //将员工按照工资由高到低排序，显示名字
        System.out.println("员工按工资由高到低排序:");
        employee.stream()
                .sorted(Comparator.comparingDouble(Employee::getSalary).reversed())
                .map(Employee::getName)
                .forEach(System.out::println);

        //将员工按照工资由低到高（工资一样则按年龄由小到大）排序，显示名字
        System.out.println("员工按工资由低到高，同工资按年龄由小到大排序:");
        employee.stream()
                .sorted(Comparator.comparingDouble(Employee::getSalary)
                        .thenComparingInt(Employee::getAge))
                .map(Employee::getName)
                .forEach(System.out::println);
    }

}
